Option Explicit On

Imports System
Imports System.Runtime.InteropServices

' FILESYSTEMIMAGELIST.VB
' File System Access for the Cold Storage component.
'
' Copyright (C) 2002 Microsoft Corporation 
' All rights reserved. 
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" 
' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
' INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
' MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
' 
' Date: 03/13/02
' Authors: Allen Wagner, Marcelo Uemura, David Willson

Friend Class StatusUpdate
    Inherits System.ComponentModel.Component

    Private Const CLASS_STATUSUPDATE As String = "StatusUpdate"
    Private theStatusBar As StatusBar

    Public Sub New( _
        ByVal container As System.ComponentModel.IContainer, _
        ByVal status As StatusBar)

        container.Add(Me, CLASS_STATUSUPDATE)
        theStatusBar = status

    End Sub


    Public Sub New( _
        ByVal container As System.ComponentModel.IContainer)

        container.Add(Me, CLASS_STATUSUPDATE)
        theStatusBar = Nothing

    End Sub

    Public Property [StatusBar]() As StatusBar
        Get
            Return theStatusBar
        End Get

        Set(ByVal Value As StatusBar)
            theStatusBar = Value
        End Set
    End Property


    Public Sub Update(ByVal updateText As String)

        If Not (StatusBar Is Nothing) Then
            theStatusBar.Text = updateText
        End If

    End Sub

End Class

Public Class FileSystemImageList
    ' Inherits System.ComponentModel.Component

    Private theShell32 As New Shell32

    Public Const FolderClosedIconIndex As Integer = 0
    Public Const FolderOpenIconIndex As Integer = 1
    Public Const FileGenericIconIndex As Integer = 2

    Private Const CLASS_FILESYSTEMIMAGELIST As String = _
        "FileSystemImageList"

    Private theExtensionIconIndex As Hashtable
    Private theSmallImageList As ImageList
    Private theLargeImageList As ImageList

    Public Sub New( _
        ByVal container As System.ComponentModel.IContainer)

        container.Add(Me, CLASS_FILESYSTEMIMAGELIST)
        InitializeComponent()
    End Sub
    Public Sub New()
        InitializeComponent()
    End Sub

    Sub InitializeComponent()

        theSmallImageList = New ImageList
        theSmallImageList.ColorDepth = _
            System.Windows.Forms.ColorDepth.Depth8Bit
        theSmallImageList.ImageSize = _
            New System.Drawing.Size(16, 16)
        theSmallImageList.TransparentColor = _
            System.Drawing.Color.Transparent

        theLargeImageList = New ImageList

        theLargeImageList.ColorDepth = _
            System.Windows.Forms.ColorDepth.Depth8Bit
        theLargeImageList.ImageSize = _
            New System.Drawing.Size(64, 64)
        theLargeImageList.TransparentColor = _
            System.Drawing.Color.Transparent

        theExtensionIconIndex = _
            New System.Collections.Hashtable

        AddFolderIcon(Convert.ToUInt32(0))

        AddFolderIcon(Convert.ToUInt32(Shell32.SHGFI_OPENICON))
        AddFileIcon(Nothing)

    End Sub

    Public ReadOnly Property SmallImageList() As ImageList
        Get
            Return theSmallImageList
        End Get
    End Property


    Public ReadOnly Property LargeImageList() As ImageList
        Get
            Return theLargeImageList
        End Get
    End Property

    Private Sub AddFolderIcon( _
        ByVal flags As System.UInt32)

        ' ToDo: Unsigned Integers such as System.UInt32
        ' are not CLS compliant types.
        flags = Convert.ToUInt32( _
            Convert.ToInt32(flags) Or _
            Convert.ToInt32(Shell32.SHGFI_ICON) Or _
            Convert.ToInt32(Shell32.SHGFI_USEFILEATTRIBUTES))

        ' Get the folder icon.
        Dim shfi As New Shell32.SHFILEINFO
        Shell32.SHGetFileInfo( _
            Nothing, _
            Shell32.FILE_ATTRIBUTE_DIRECTORY, _
            shfi, _
            Convert.ToUInt32( _
                System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
            flags)

        Dim icon As System.Drawing.Icon = _
            System.Drawing.Icon.FromHandle(shfi.hIcon)
        theLargeImageList.Images.Add(icon)
        User32.DestroyIcon(shfi.hIcon)

        ' Get the small folder icon.
        flags = Convert.ToUInt32(Convert.ToInt32(flags) Or _
            Convert.ToInt32(Shell32.SHGFI_SMALLICON))

        Shell32.SHGetFileInfo( _
            Nothing, _
            Shell32.FILE_ATTRIBUTE_DIRECTORY, _
            shfi, _
            Convert.ToUInt32( _
                System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
            flags)

        icon = System.Drawing.Icon.FromHandle(shfi.hIcon)
        theSmallImageList.Images.Add(icon)
        User32.DestroyIcon(shfi.hIcon)

    End Sub

    Private Function AddFileIcon(ByVal name As String) As Integer

        ' Get the large generic file icon.
        Dim shfi As New Shell32.SHFILEINFO
        Shell32.SHGetFileInfo( _
            name, _
            Shell32.FILE_ATTRIBUTE_NORMAL, _
            shfi, _
            System.Convert.ToUInt32( _
                Runtime.InteropServices.Marshal.SizeOf(shfi)), _
            Convert.ToUInt32( _
                Convert.ToInt32( _
                    Shell32.SHGFI_USEFILEATTRIBUTES) Or _
                Convert.ToInt32(Shell32.SHGFI_ICON)))

        Dim icon As System.Drawing.Icon = _
            System.Drawing.Icon.FromHandle(shfi.hIcon)
        theLargeImageList.Images.Add(icon)
        User32.DestroyIcon(shfi.hIcon)

        ' Get the small generic file icon.
        shfi = New Shell32.SHFILEINFO
        Shell32.SHGetFileInfo( _
            name, _
            Shell32.FILE_ATTRIBUTE_NORMAL, _
            shfi, _
            Convert.ToUInt32( _
                System.Runtime.InteropServices.Marshal.SizeOf(shfi)), _
            Convert.ToUInt32( _
                Convert.ToInt32(Shell32.SHGFI_ICON) Or _
                Convert.ToInt32(Shell32.SHGFI_SMALLICON) Or _
                Convert.ToInt32(Shell32.SHGFI_USEFILEATTRIBUTES)))

        icon = System.Drawing.Icon.FromHandle(shfi.hIcon)
        theSmallImageList.Images.Add(icon)
        User32.DestroyIcon(shfi.hIcon)

        If Not (name Is Nothing) Then
            Dim iconindex As Integer = _
            SmallImageList.Images.Count - 1

            theExtensionIconIndex.Add(name, iconindex)
            Return iconindex

        End If

        Return FileGenericIconIndex

    End Function

    Public Function GetTheFileIconIndex( _
        ByVal filename As String) As Integer
        Dim index As Integer = filename.LastIndexOf(".")

        If index > -1 Then
            Dim extension As String = filename.Substring(index)

            If Not theExtensionIconIndex.ContainsKey(extension) Then
                Return AddFileIcon(extension)
            Else
                Return CInt(theExtensionIconIndex(extension))
            End If

        Else

            ' No extension known. Use the generic file icons.
            Return FileGenericIconIndex

        End If

    End Function

End Class

Public Class Shell32

    Public Const MAX_PATH As Integer = 256

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SHITEMID

        <MarshalAs(UnmanagedType.LPArray)> _
            Public cb As System.UInt16
        Public abID() As Byte

    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure ITEMIDLIST

        Public mkid As SHITEMID

    End Structure

    <StructLayout(LayoutKind.Sequential)> _
        Public Structure BROWSEINFO

        Public hwndOwner As IntPtr
        Public pidlRoot As IntPtr
        Public pszDisplayName As IntPtr
        <MarshalAs(UnmanagedType.LPTStr)> _
            Public lpszTitle As String
        Public ulFlags As System.UInt32
        Public lpfn As IntPtr
        Public lParam As Integer
        Public iImage As IntPtr

    End Structure

    ' Browsing for directory.
    ' Note: Function System.Convert.ToUInt32 is not CLS Compliant.
    Public Shared BIF_RETURNONLYFSDIRS As System.UInt32 = _
        Convert.ToUInt32(&H1)
    Public Shared BIF_DONTGOBELOWDOMAIN As System.UInt32 = _
        Convert.ToUInt32(&H2)
    Public Shared BIF_STATUSTEXT As System.UInt32 = _
        Convert.ToUInt32(&H4)
    Public Shared BIF_RETURNFSANCESTORS As System.UInt32 = _
        Convert.ToUInt32(&H8)
    Public Shared BIF_EDITBOX As System.UInt32 = _
        Convert.ToUInt32(&H10)
    Public Shared BIF_VALIDATE As System.UInt32 = _
        Convert.ToUInt32(&H20)
    Public Shared BIF_NEWDIALOGSTYLE As System.UInt32 = _
        Convert.ToUInt32(&H40)
    Public Shared BIF_USENEWUI As System.UInt32 = _
        Convert.ToUInt32(&H50)
    Public Shared BIF_BROWSEINCLUDEURLS As System.UInt32 = _
        Convert.ToUInt32(&H80)
    Public Shared BIF_BROWSEFORCOMPUTER As System.UInt32 = _
        Convert.ToUInt32(&H1000)
    Public Shared BIF_BROWSEFORPRINTER As System.UInt32 = _
        Convert.ToUInt32(&H2000)
    Public Shared BIF_BROWSEINCLUDEFILES As System.UInt32 = _
        Convert.ToUInt32(&H4000)
    Public Shared BIF_SHAREABLE As System.UInt32 = _
        Convert.ToUInt32(&H8000)

    Public szDisplayName As String

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SHFILEINFO

        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As System.UInt32
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String

    End Structure

    ' Get Icon
    Public Shared SHGFI_ICON As System.UInt32 = _
        Convert.ToUInt32(&H100)
    ' Get Display Name
    Public Shared SHGFI_DISPLAYNAME As System.UInt32 = _
        Convert.ToUInt32(&H200)
    ' Get Type Name
    Public Shared SHGFI_TYPENAME As System.UInt32 = _
        Convert.ToUInt32(&H400)
    ' Get Attributes
    Public Shared SHGFI_ATTRIBUTES As System.UInt32 = _
        Convert.ToUInt32(&H800)
    ' Get Icon Location
    Public Shared SHGFI_ICONLOCATION As System.UInt32 = _
        Convert.ToUInt32(&H1000)
    ' Return Executable Type
    Public Shared SHGFI_EXETYPE As System.UInt32 = _
        Convert.ToUInt32(&H2000)
    ' Get System Icon Index
    Public Shared SHGFI_SYSICONINDEX As System.UInt32 = _
        Convert.ToUInt32(&H4000)
    ' Put a link overlay on the icon.
    Public Shared SHGFI_LINKOVERLAY As System.UInt32 = _
        Convert.ToUInt32(&H8000)
    ' Show the icon in the selected state.
    Public Shared SHGFI_SELECTED As System.UInt32 = _
        Convert.ToUInt32(&H10000)
    ' Get only specified attributes.
    Public Shared SHGFI_ATTR_SPECIFIED As System.UInt32 = _
        Convert.ToUInt32(&H20000)
    ' Get Large Icon
    Public Shared SHGFI_LARGEICON As System.UInt32 = _
        Convert.ToUInt32(&H0)
    ' Get Small Icon
    Public Shared SHGFI_SMALLICON As System.UInt32 = _
        Convert.ToUInt32(&H1)
    ' Get Open Icon
    Public Shared SHGFI_OPENICON As System.UInt32 = _
        Convert.ToUInt32(&H2)
    ' Get Shell Size Icon
    Public Shared SHGFI_SHELLICONSIZE As System.UInt32 = _
        Convert.ToUInt32(&H4)
    ' The pszPath is a PIDL.
    Public Shared SHGFI_PIDL As System.UInt32 = _
        Convert.ToUInt32(&H8)
    ' Use Passed dwFileAttribute
    Public Shared SHGFI_USEFILEATTRIBUTES As System.UInt32 = _
        Convert.ToUInt32(&H10)
    ' Apply the appropriate overlays.
    Public Shared SHGFI_ADDOVERLAYS As System.UInt32 = _
        Convert.ToUInt32(&H20)
    ' Get the index of the overlay.
    Public Shared SHGFI_OVERLAYINDEX As System.UInt32 = _
        Convert.ToUInt32(&H40)

    Public Shared FILE_ATTRIBUTE_DIRECTORY As System.UInt32 = _
        Convert.ToUInt32(&H10)
    Public Shared FILE_ATTRIBUTE_NORMAL As System.UInt32 = _
        Convert.ToUInt32(&H80)

    <DllImport("Shell32.dll")> _
    Public Shared Function SHGetFileInfo( _
    ByVal pszPath As String, _
    ByVal dwFileAttributes As System.UInt32, _
        ByRef psfi As SHFILEINFO, _
    ByVal cbFileInfo As System.UInt32, _
    ByVal uFlags As System.UInt32) As IntPtr
    End Function

    <DllImport("Shell32.dll")> _
    Public Shared Function SHBrowseForFolder( _
        ByRef lpbi As BROWSEINFO) As IntPtr
    End Function

    <DllImport("Shell32.dll")> _
    Public Shared Function SHGetPathFromIDList( _
    ByVal pidl As IntPtr, _
    ByVal pszPath As IntPtr) As System.UInt32
    End Function

End Class

Public Class User32

    <DllImport("User32.dll")> _
    Public Shared Function DestroyIcon( _
        ByVal hIcon As IntPtr) As Integer
    End Function

End Class


